{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "cfa24e9b-6bf4-4f38-9058-579cab36562f",
   "metadata": {},
   "source": [
    "Copyright 2023 Google LLC. SPDX-License-Identifier: Apache-2.0\n",
    "\n",
    "Licensed under the Apache License, Version 2.0 (the \"License\"); you may not use this file except in compliance with the License. You may obtain a copy of the License at\n",
    "\n",
    "https://www.apache.org/licenses/LICENSE-2.0\n",
    "\n",
    "Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ff375a79",
   "metadata": {},
   "source": [
    "# layout model basic inference"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "64cc2666",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pickle\n",
    "import torch\n",
    "from models.layout import model_full\n",
    "from utils import camera_util\n",
    "from baukit import renormalize\n",
    "\n",
    "torch.set_grad_enabled(False)\n",
    "device = 'cuda'\n",
    "\n",
    "full_model = model_full.ModelFull('pretrained/model_terrain.pkl', 'pretrained/model_sky_360.pkl').to(device).eval()\n",
    "G_layout = full_model.terrain_model.layout_model\n",
    "torch.manual_seed(1)\n",
    "z = torch.randn(1, G_layout.layout_generator.z_dim, device=device)\n",
    "c = None\n",
    "camera_params = camera_util.get_full_image_parameters(\n",
    "    G_layout, G_layout.layout_decoder_kwargs.nerf_out_res, batch_size=1, device=device)\n",
    "output = full_model(z, c, camera_params)\n",
    "renormalize.as_image(output['rgb_overlay_upsample'][0])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7906e2b8",
   "metadata": {},
   "source": [
    "# triplane model basic inference"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9ad638ee",
   "metadata": {},
   "outputs": [],
   "source": [
    "from models.triplane import model_full\n",
    "from utils.soat_util_triplane import construct_intrinsics\n",
    "\n",
    "full_model = model_full.ModelFull('pretrained/model_triplane.pkl', 'pretrained/model_sky_360.pkl').to(device).eval()\n",
    "torch.manual_seed(1)\n",
    "z = torch.randn([1, full_model.ground.z_dim]).to(device)  # latent codes\n",
    "intrinsics = construct_intrinsics(60).to(device)\n",
    "extrinsics = full_model.world2cam_poses[1].inverse().to(device) # change world2cam -> cam2world pose\n",
    "c = torch.cat([extrinsics.reshape(1, -1), intrinsics.reshape(1, -1)], dim=-1)\n",
    "output = full_model(z, c, render_sky='True')\n",
    "renormalize.as_image(output['composite'][0])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "persistentnature",
   "language": "python",
   "name": "persistentnature"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
